package com.jonasgreen.core.client.service;


import com.jonasgreen.core.client.*;
import com.jonasgreen.core.client.language.*;
import com.jonasgreen.core.client.model.*;
import com.jonasgreen.core.client.model.keys.*;


/**
 *
 */
public class Service {


    private static ServerWrapper server = new ServerWrapper();

    public static void log(LogAction.Type type, String msg, Throwable t) {
        executeWithRetry(new LogAction(msg, type, t), new SilentCallBack() {
            public void success(Object result) {
                //ignore
            }

            public void fail(Throwable t) {
                //ignore
            }
        });
    }


    public static void getUUID(int numberOfIds, CallBack<UUIDResult> cb) {
        executeWithRetry(new GetUUIDAction(numberOfIds), cb);
    }


    //CONSTANT

    public static void createConstant(CreateConstant cr, CallBack<SingleResult<Constant>> cb) {
        cb.startProgressBar("Creating " + cr.getGroup().getText());
        executeWithRetry(new CreateConstantAction(cr), cb);
    }

    public static void getConstants(GroupConstants gc, CallBack<ListResult<Constant>> cb) {
        IntegerSingleFinder f = new IntegerSingleFinder("Integer", "group", SingleFinder.Operator.EQUAL_TO, gc.getValue());
        f.setOrdering("name asc");
        findBy(FindIn.constant, f, "", cb);
    }


    public static void createWaitingEmail(String email, final CallBack<VoidResult> cb) {
        cb.startProgressBar("sending email address");
        executeWithRetry(new CreateWaitingMail(email), cb);
    }



    //Ping - to avoid servlet to go to sleep while app is open.

    public static void pingServer(CallBack<VoidResult> cb) {
        cb.startProgressBar("Pinger");
        executeWithRetry(new PingAction(), cb);
    }

    public static void printBackEnd(CallBack<VoidResult> cb) {
        //cb.startProgressBar("PrintBackEnd call");
        //executeWithRetry(new PrintBackEndAction(), cb);
    }


    //GENERICS

    public static <M extends Model> void findBy(FindIn findIn, Finder finder, String text, final CallBack<ListResult<M>> cb) {
        cb.startProgressBar(text);
        executeWithRetry(new FindByAction(findIn, finder), cb);
    }

    public static <M extends Model> void getAll(FindIn findIn, String text, final CallBack<ListResult<M>> cb) {
        cb.startProgressBar(text);
        executeWithRetry(new GetAllAction(findIn), cb);
    }

    public static <M extends Model> void get(FindIn findIn, EntityId indexKey, String text, final CallBack<SingleResult<M>> cb) {
        cb.startProgressBar(text);
        executeWithRetry(new GetAction(findIn, indexKey), cb);
    }

    public static void delete(FindIn findIn, EntityId indexKey, String text, final CallBack<VoidResult> cb) {
        cb.startProgressBar(text);
        executeWithRetry(new DeleteAction(findIn, indexKey), cb);
    }

    public static void update(FindIn findIn, Model m, String text, final CallBack<SingleResult> cb) {
        cb.startProgressBar(text);
        executeWithRetry(new UpdateAction(findIn, m), cb);
    }

    public static void executeWithRetry(Action a, CallBack cb) {
        a.setLanguage(Language.language.getValue());
        //cb.setRetry(a);
        server.execute(a, cb);
    }

    public static void resend(Action a, CallBack cb) {
        server.execute(a, cb);
    }

}
